package com.aiit.controller.user;

import com.aiit.constant.StatusConstant;
import com.aiit.dto.TeaOrderDTO;
import com.aiit.entity.Tea;
import com.aiit.result.Result;
import com.aiit.service.TeaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/tea")
@Slf4j
@Api(tags = "客户端：茶品相关接口")
public class TeaController2 {

    @Autowired
    private TeaService teaService;

    /**
     * 条件查询
     *
     * @param
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据商品名称查询套餐")
    @Cacheable(cacheNames = "teaCache2", key = "#name != null ? #name : 'default_name'")
    public Result<List<Tea>> list(String name) {
        Tea tea = new Tea();
        tea.setName(name); // 设置商品名称
        tea.setStatus(StatusConstant.ENABLE);
        System.out.println("已完成用户查询");

        List<Tea> list = teaService.list(tea);
        return Result.success(list);
    }

    @PostMapping("/order/place")
    @ApiOperation("订单插入")
    public Result save(@RequestBody TeaOrderDTO teaOrderDTO, @RequestParam String username) {
        log.info("订单插入：{}", teaOrderDTO);
        teaOrderDTO.setUsername(username);
        System.out.println("当前线程的id:" + Thread.currentThread().getId());
        teaService.saveOrder(teaOrderDTO);
        return Result.success();
    }


}
